const Koa = require('koa');
const ejs = require('koa-ejs');  // 引入 koa-ejs 中间件
const path = require('path');
const { connect, initSchemas } = require('./db/init');
const mongoose = require('mongoose');


const app = new Koa();

(async () => {
  await connect();
  initSchemas();  // 初始化所有 schemas
  const Movie = mongoose.model('Movie'); // 获取 Movie 模型
  // await Movie.deleteMany();

  require('./tasks/movie');  // 引入 movie.js

  // 测试代码

  const resMovie = await Movie.find();
  console.log("The Movie is : ", resMovie);

})();

ejs(app, {
  root: path.resolve(__dirname, 'views'),  // 模板根目录
  layout: false,  // 是否对根目录下的文件布局
  viewExt: 'ejs',  // 后缀名
  cache: false  // 是否缓存在服务端
})

app.use(async (ctx, next) => {
  await ctx.render('index', {
    title: 'koa 电影预告片首页',
    name: '臭臭侠',
    msg: '看电影不要钱'
  })
});

app.listen(3000, () => {
  console.log('server running on http://localhost:3000');
});
